
package com.funo.government.info.view;

import java.util.ArrayList;

import android.app.ProgressDialog;
import android.app.TabActivity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.Toast;

import com.funo.government.info.MainApplication;
import com.funo.government.info.R;
import com.funo.government.info.bean.NavItem;
import com.funo.government.info.constant.Constant;
import com.funo.government.info.utils.BusinessException;
import com.funo.government.info.utils.BusinessResolver.BusinessCallback;
import com.funo.government.info.utils.GovernmentResolver;
import com.funo.government.info.utils.NetUtils;
import com.funo.government.info.utils.PreferenceUtils;
import com.funo.government.info.utils.RequestTask;
import com.funo.government.info.utils.Utils;

@SuppressWarnings("deprecation")
public class OtherActivity extends TabActivity {

    private TabHost mTabHost;

    private RequestTask mGetNavTask;

    private ProgressDialog progressDialog;

    private BroadcastReceiver mloadNavReceiver;

    private int selectTabPosition;

    public static final String ACTION_UPDATE_NAV = "action_update_nav_government";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_other);
        MainApplication ma = (MainApplication) getApplicationContext();
        ma.addActivity(this);
        if (!NetUtils.isConnected(this)) {
            Toast.makeText(this, R.string.not_network, Toast.LENGTH_LONG).show();
            return;
        }

        mTabHost = (TabHost) findViewById(android.R.id.tabhost);

        mTabHost.setup();

        progressDialog = new ProgressDialog(this, ProgressDialog.STYLE_SPINNER);
        progressDialog.setMessage(getString(R.string.drop_down_list_footer_loading_text));
        String areaId = PreferenceUtils.getLocalAreaId(this);
        loadNav(areaId);
        Utils.ProcessPullService(this);
        mloadNavReceiver = new LoadNavReceiver();
        registerReceiver(mloadNavReceiver, new IntentFilter(ACTION_UPDATE_NAV));

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onMenuItemSelected(int featureId, MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_exit:
                finish();
                break;
        }
        return super.onMenuItemSelected(featureId, item);
    }

    public void loadNav(String areaId) {

        progressDialog.show();
        mGetNavTask = GovernmentResolver.navList(areaId,
                new BusinessCallback<ArrayList<NavItem>>() {

                    @Override
                    public void onSuccess(ArrayList<NavItem> t) {
                        progressDialog.dismiss();
                        buildTab(t);
                    }

                    @Override
                    public void onError(BusinessException e) {
                        progressDialog.dismiss();
                        Toast.makeText(OtherActivity.this, R.string.cannot_connect_server,
                                Toast.LENGTH_SHORT).show();
                        e.printStackTrace();
                    }
                });
    }

    public void buildTab(ArrayList<NavItem> navs) {
        View view;
        TextView tvTitle;
        ImageView ivIcon;
        Intent intent;
        for (NavItem item : navs) {
            intent = new Intent(this, WebActivity.class);
            view = getLayoutInflater().inflate(R.layout.tabhost_item, null);
            tvTitle = (TextView) view.findViewById(R.id.tvTitle);
            ivIcon = (ImageView) view.findViewById(R.id.ivIcon);
            tvTitle.setText(item.chnlname);
            Integer resId = Constant.NAV_ICON_MAP.get(item.chnlId);
            if (resId != null) {
                ivIcon.setImageResource(resId);
            } else {
                ivIcon.setImageResource(R.drawable.tab_home);
            }
            view.setTag(item.chnlname);
            intent.putExtra("link", item.linkUrl);
            mTabHost.addTab(mTabHost.newTabSpec(item.chnlname).setIndicator(view)
                    .setContent(intent));

        }
        view = getLayoutInflater().inflate(R.layout.tabhost_item, null);
        tvTitle = (TextView) view.findViewById(R.id.tvTitle);
        ivIcon = (ImageView) view.findViewById(R.id.ivIcon);
        tvTitle.setText(R.string.more);
        ivIcon.setImageResource(R.drawable.tab_more);
        intent = new Intent(this, MoreActivity.class);
        mTabHost.addTab(mTabHost.newTabSpec("more").setIndicator(view).setContent(intent));
        int length = mTabHost.getTabWidget().getChildCount();

        for (int i = 0; i < length; i++) {
            final int index = i;
            mTabHost.getTabWidget().getChildAt(i).setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    if (selectTabPosition == index) {
                        String tag = (String) v.getTag();
                        Intent intent = new Intent(OtherActivity.this, WebActivity.class);
                        intent.putExtra("reload", "aaa");
                        intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
                        getLocalActivityManager().startActivity(tag, intent);
                    }
                    mTabHost.setCurrentTab(index);
                    selectTabPosition = index;
                }
            });

        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(mloadNavReceiver);
        if (mGetNavTask != null) {
            mGetNavTask.cancel(true);
            mGetNavTask = null;
        }
    }

    private class LoadNavReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            String areaId = intent.getStringExtra(Constant.PRE_KEY_AREA_ID);
            if (ACTION_UPDATE_NAV.equalsIgnoreCase(action)) {
                loadNav(areaId);
            }
        }
    }

}
